// pages/myorder/myorder.js
const app = getApp()
Page({
  data: {
    list0: '',
    list1: '',
    currentData: 0,
    currentData2: 0,
    currentData1: 5,
    seelocation: false,
    curlocation: 0,

  },
  onLoad: function(options) {
    this.getorder(1)
  },
  onShow: function (options){
    this.getorder(1)
  },
  //点击切换，滑块index赋值私人定制切换
  checkCurrent: function(e) {
    const that = this;
    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else if (that.data.currentData1 == 5) {
      that.setData({
        currentData: e.target.dataset.current,
      })
      let num = Number(this.data.currentData) + 1;
      console.log(num)
      if (num == 4) {
        that.getorder('');
      } else {
        that.getorder(num);
      }

    }
  },
  checkCurrent2(e) { //企业定制切换
    const that = this;
    console.log(e.target.dataset.current)
    this.setData({
      currentData2: e.target.dataset.current
    })
    let num = e.target.dataset.current;
    if (num == 1) {
      this.getcampanyorder(Number(num) + 2);
    } else if (num == 2) {
      this.getcampanyorder(Number(num) + 2);
    } else if (num == 0) {
      this.getcampanyorder(num);
    }


  },
  checkCurrent1(e) { //切换s私人企业定制
    console.log(e.target.dataset.current)
    this.setData({
      currentData1: e.target.dataset.current,
    })
    if (e.target.dataset.current == 6) {
      this.getcampanyorder(0);
      this.setData({
        currentData2: 0,
      })
    } else {
      this.getorder(1);
    }
  },
  derdetail: function(e) { //订单详情审核中
    let oid = e.currentTarget.dataset.oid;
    let uvid = e.currentTarget.dataset.uvid;
    let picstatus = e.currentTarget.dataset.picstatus;
    wx.navigateTo({
      url: '/pages/orderdetail/orderdetail?oid=' + oid + '&uvid=' + uvid + '&picstatus=' + picstatus
    })
  },
  companydeail(e) {
    let eoid = e.currentTarget.dataset.eoid;
    wx.navigateTo({
      url: '/pages/companydetail/companydetail?eoid=' + eoid,
    })
  },
  moneyback(e) { //退款审核
    let oid = e.currentTarget.dataset.oid;
    let uvid = e.currentTarget.dataset.uvid;
    let amount = e.currentTarget.dataset.amount;
    let total = e.currentTarget.dataset.total;
    wx.navigateTo({
      url: '/pages/moneyback/moneyback?oid=' + oid + '&uvid=' + uvid + '&amount=' + amount + '&total=' + total
    })
  },
  derdetaildxg(e) { //待发货详情
    let oid = e.currentTarget.dataset.oid;
    let uvid = e.currentTarget.dataset.uvid;
    wx.navigateTo({
      url: '/pages/orderdetaildxg/orderdetaildxg?oid=' + oid + '&uvid=' + uvid
    })
  },
  derdetaildsh(e) { //待收货
    let oid = e.currentTarget.dataset.oid;
    let uvid = e.currentTarget.dataset.uvid;
    wx.navigateTo({
      url: '/pages/orderdetaildsh/orderdetaildsh?oid=' + oid + '&uvid=' + uvid
    })
  },
  alldeail(e) { //全部订单查看详情
    // let oid = e.currentTarget.dataset.oid;
    // let uvid = e.currentTarget.dataset.uvid;
    let status = e.currentTarget.dataset.status;
    if (status == 1) {
      this.derdetail(e) //审核中
    } else if (status == 2) {
      this.derdetaildxg(e)
    } else if (status == 3) {
      this.derdetaildsh();
    }
  },
  getorder(status) { //订单列表
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/order/getOrderList',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        status: status,
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function(res) {
        that.setData({
          list0: res.data.data
        })
        console.log(res.data.data)
      },

    })
  },
  getcampanyorder(status) { //企业订单列表
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/eorder/foundOrderList',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        status: status,
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function(res) {
        that.setData({
          list1: res.data.data
        })
        console.log(res.data.data)
      },

    })
  },
  repay(e) { //立即付款
    let oid = e.currentTarget.dataset.oid;
    this.paymoney(oid);
  },
  paymoney(oid) { // 支付
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/pay/doOrder',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        oid: oid,
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function(res) {
        console.log(res.data)
        that.doWxPay(res.data);
      },

    })
  },
  seelocation(e) { //查看物流
    let index = e.currentTarget.dataset.index;
    let oid = e.currentTarget.dataset.oid;
    let data = {
      tid: oid
    }
    this.setData({
      seelocation: !this.data.seelocation,
      curlocation: index
    })
    let that = this;
    wx.request({ //传后台
      url: 'https://jxbscbd.ijovo.com/gateway/transfer/jxb/openapi2/query_logistics',
      method: 'POST',
      header: {
        "Content-Type": "application/json",
        "Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIwMDA0MDAyMiIsImlzcyI6ImpveWJvIiwiZXhwIjoxNTk5ODA3NzE1LCJpYXQiOjE1Njg3MDM3MTUsInJvbCI6IlJPTEVfVVNFUiwifQ.xATLfZu4mYJqRMuX7OjHb6cG21CrULPB3KCL8-79Wo5LUTQyKwAR_DN7UoqHPQ81lZO6j4YNvGEKIWCpMZhd3w"
      },
      data: data,
      contentType: "application/x-www-form-urlencoded",
      success: function(res) {
        if (res.code == 200) {
          thst.setData({
            logistics_no: res.data.rows.logistics_no,
            logistics_name: res.data.rows.logistics_name
          })
        }
      },

    })

  },
  doWxPay(param) { //微信支付
    let that = this;
    wx.requestPayment({
      timeStamp: param.timeStamp, //记住，这边的timeStamp一定要是字符串类型的，不然会报错
      nonceStr: param.nonceStr,
      package: param.package,
      signType: 'MD5',
      paySign: param.paySign,
      success: function(event) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000

        });
        that.paysucess(); //支付成功
      },

      fail: function(error) {
        wx.showToast({
          title: '支付失败',
          duration: 2000,
          icon:'none'

        });
      },
      complete: function() {
        console.log("pay complete")
      }
    });
  },
  paysucess() { //支付成功跳转
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/order/isComment',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function(res) {
        let pstatus = res.data.data.pstatus;
        if (pstatus == '0') { //已评价
          wx.navigateTo({
            url: '/pages/paymore/paymore',
          })
        } else {
          wx.navigateTo({
            url: '/pages/ratestart/ratestart',
          })
        }
      },

    })
  },
  goindex() { //去定制页面
    wx.navigateTo({
      url: '/pages/persondz/persondz',
    })
  },
  gocompany(){
    wx.navigateTo({
      url: '/pages/company/company',
    })
  }
})